package com.itheima.server.controller;

import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.SettingsService;
import com.itheima.server.service.UserInfoService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    /**
     *接口名称：用户资料 - 读取
     *接口路径：GET/users
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token){
        //1.判断userId是否为空，如果为空则查询当前登陆者的用户的信息
        if(userID==null){
            //Claims claims = JwtUtils.getClaims(token); //claims就是map集合，里面有id与mobile
            //Integer id = (Integer) claims.get("id");
            userID = UserHolder.getId();
        }
        //2. 根据id查询用于的信息
        UserInfoVo userInfoVo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }


    /**
     *接口名称：用户资料 - 保存
     *接口路径：PUT/users
     */
    @PutMapping
    public ResponseEntity userInfo(@RequestBody  UserInfo userInfo, @RequestHeader("Authorization") String token){
        //1.解析token得到用户id
        Claims claims = JwtUtils.getClaims(token); //claims就是map集合，里面有id与mobile
        Integer id = (Integer) claims.get("id");
        //2.把用户的id设置到userinfo对象中
        userInfo.setId(Long.valueOf(id));
        //3.把userInfo信息对象交给userInfoService
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);

    }


    /**
     *接口名称：用户通用设置
     *接口路径：GET/users/settings
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }


    /**
     *接口名称：设置陌生人问题
     *接口路径：POST/users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map<String,String> paramMap){
        String content = paramMap.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     *接口名称：黑名单 - 翻页列表
     *接口路径：GET/users/blacklist
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = settingsService.queryBlackList(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     *接口名称：黑名单 - 移除
     *接口路径：DELETE/users/blacklist/:uid
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeBlackUser(@PathVariable("uid") Long blackUserId){
        settingsService.removeBlackUser(blackUserId);
        return ResponseEntity.ok(null);
    }

}
